import { createRouter, createWebHistory } from 'vue-router';
import store from '../store';
import Reservation from '../components/Reservation.vue';
import User from '../components/User.vue';
import Login from '../components/Login.vue';
import Register from '../components/Register.vue';
import GetReservation from '../components/GetReservation.vue';

const routes = [
  {
    path: '/',
    component: Reservation
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/User',
    name: 'User',
    component: User,
    meta: { requiresAuth: true } // 添加meta字段用于标记需要认证的路由
  },
  {
    path: '/Register',
    name: 'Register',
    component: Register
  },
  {
    path: '/Reservation',
    name: 'Reservation',
    component: GetReservation,
    meta: { requiresAuth: true }
  }
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
});

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!store.getters['auth/isLoggedIn']) {
      store.commit('auth/TOGGLE_LOGIN_DIALOG', true);
      next(false); // 阻止导航
    } else {
      next(); // 继续导航
    }
  } else {
    next(); // 继续导航
  }
});

export default router;